﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace ZCrawler
{
    [Serializable()]
    public class ZResultSet
    {
        private List<ZResult> set;

        public ZResultSet()
        {
            set = new List<ZResult>();
        }
        public void Add(ZResult r)
        {
            set.Add(r);
        }
        public ZResult this[int k]
        {
            get
            {
                return set[k];
            }
            set
            {
                set[k] = value;
            }
        }
        public DataTable results()
        {
            DataTable dt = new DataTable("data");
            if (set.Count == 0)
                return dt;
            ZResult result = set.First();
            result.DATA.Keys.ToList().ForEach(k =>
                {
                    dt.Columns.Add(k.ToString());
                });
            set.ForEach(s =>
                {
                    DataRow row = dt.NewRow();
                    s.DATA.Keys.ToList().ForEach(k =>
                        {
                            row[k.ToString()] = s.DATA[k];
                        });
                    dt.Rows.Add(row);
                });
            return dt;
        }
        public int Count
        {
            get
            {
                return set.Count;
            }
        }
        public void Clear()
        {
            set.Clear();
        }

        public IEnumerator<ZResult> GetEnumerator()
        {
            return set.GetEnumerator();
        }
    }
}
